package com.zhhq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhhq.model.Goods;
import com.zhhq.model.Result;
import com.zhhq.service.IGoodsService;
import com.zhhq.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by HeChaobin on 2017-07-22.
 */
@RestController
@RequestMapping(value = "/goods")
@Api(value = "/goods", description = "商品操作相关接口")
public class GoodsController {

    @Autowired
    private IGoodsService iGoodsService;

    @GetMapping(value = "/{pagesize}/{page}/{priceasc}")
    @ApiOperation(
            value = "获取商品列表",
            notes = "获取指定页商品")
    public Result selectGoods(@PathVariable Integer pagesize, @PathVariable Integer page, @PathVariable Integer priceasc) {

        String orderByString="price asc";
        if (priceasc != null&&priceasc!=1) {
            orderByString="price desc";
        }

        PageHelper.startPage(page, pagesize,orderByString);
        List<Goods> goods = iGoodsService.selectGoods();
        PageInfo info = new PageInfo(goods);

        return ResultUtil.success(info);
    }

    @GetMapping(value = "/{pagesize}/{page}/{priceasc}/{keyword}")
    @ApiOperation(
            value = "搜索商品列表",
            notes = "获取指定页商品")
    public Result searchGoods(@PathVariable Integer pagesize, @PathVariable Integer page, @PathVariable Integer priceasc, @PathVariable String keyword) {

        String orderByString="price asc";
        if (priceasc != null&&priceasc!=1) {
            orderByString="price desc";
        }


        PageHelper.startPage(page, pagesize,orderByString);
        List<Goods> goods = iGoodsService.search(keyword);
        //boolean isUp = true; // 价格从低到高排序

//            if (keyword != null&&keyword!="") {
//                goods = iGoodsService.selectGoods();
//            } else {
//                goods = iGoodsService.selectGoods();
//                if (isUp) {
//                    Collections.sort(goods);
//                } else {
//                    Collections.reverse(goods);
//                }
//            }


        PageInfo info = new PageInfo(goods);

        return ResultUtil.success(info);
    }
}
